package mhwi.cards.taidao;

import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.actions.common.DamageAction;
import com.megacrit.cardcrawl.actions.common.RemoveSpecificPowerAction;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import mhwi.actions.RemoveCurrentQiRenPowerAction;
import mhwi.cards.BaDaoCard;
import mhwi.helper.ModHelper;
import mhwi.powers.*;

import static mhwi.constants.ResourcesConstant.*;
import static mhwi.enums.ModEnums.PlayerColorEnum.CARD_COLOR_HUNTER_TAIDAO;

public class QiRenDouGeCard extends BaDaoCard {
    private static final String CLASS_NAME = QiRenDouGeCard.class.getSimpleName();
    public static final String ID = ModHelper.makeId(CLASS_NAME);
    private static final CardStrings CARD_STRINGS = CardCrawlGame.languagePack.getCardStrings(ID);
    private static final String NAME = CARD_STRINGS.NAME;
    private static final String IMG_PATH = ModHelper.makeFileName(PATH_IMG_CARDS_TAIDAO, CLASS_NAME, EXTENSION_PNG);
    private static final String DESCRIPTION = CARD_STRINGS.DESCRIPTION;
    private static final CardColor COLOR = CARD_COLOR_HUNTER_TAIDAO;

    private static final int COST = -1;
    private static final CardType TYPE = CardType.ATTACK;
    private static final CardRarity RARITY = CardRarity.UNCOMMON;
    private static final CardTarget TARGET = CardTarget.ENEMY;

    public QiRenDouGeCard() {
        super(ID, NAME, IMG_PATH, COST, DESCRIPTION, TYPE, COLOR, RARITY, TARGET);
        this.baseDamage = 8;
        this.tags.add(CardTags.STRIKE);

        // X费卡必须设置以下属性
        this.isCostModifiedForTurn = false;
        this.energyOnUse = -1; // 启用X费机制
    }

    // 升级卡牌
    @Override
    public void upgrade() {
        if (!upgraded) {
            upgradeName();
            upgradeDamage(2);
            this.rawDescription = CARD_STRINGS.UPGRADE_DESCRIPTION;
            initializeDescription();
        }
    }

    // 使用卡牌
    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        // 计算实际消耗能量（兼容化学X等效果）
        int energyUsed = (this.energyOnUse == -1) ? p.energy.energy : this.energyOnUse;
        energyUsed = Math.max(0, energyUsed); // 防止负数

        // 循环造成多次伤害
        for (int i = 0; i < energyUsed; i++) {
            addToBot(new DamageAction(m, new DamageInfo(p, this.damage, this.damageTypeForTurn)));
        }
        if (upgraded) {
            addToBot(new DamageAction(m, new DamageInfo(p, this.damage, this.damageTypeForTurn)));
        }
        AbstractDungeon.actionManager.addToTop(new ApplyPowerAction(p, p, new HuiQiPower(p, 2), 2));
        AbstractDungeon.actionManager.addToTop(new RemoveCurrentQiRenPowerAction(p));
        AbstractDungeon.actionManager.addToTop(new RemoveSpecificPowerAction(p, p, DengLongPower.POWER_ID));
    }

    @Override
    public boolean canUse(AbstractPlayer p, AbstractMonster m) {
        return super.canUse(p, m) && ModHelper.hasPowers(DengLongPower.POWER_ID) && ModHelper.hasQiRenPowers();
    }
}